package com.cskaoyan.login;

import com.cskaoyan.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

// 登录的案例
public class LoginDemo2 {



    // 主方法
    public static void main(String[] args) {

//        Boolean ret = login("changfeng", "bmw");

        // select * from user where username = 'changfeng' and password = 'sss' or '1=1';
        Boolean ret = login("seggead", "sffwgsdad' or '1=1");

        if (ret) {
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败");
        }

    }


    // 登录的方法
    public static Boolean login(String username,String password) {

        try {
            // 获取连接
            Connection connection = JDBCUtils.getConnection();

            // 获取PrepareStatement对象
            PreparedStatement preparedStatement = connection.prepareStatement("select * from user where username = ? and password = ?");

            // 设值
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);

            // 执行sql
            ResultSet resultSet = preparedStatement.executeQuery();

            // 解析结果集
            if (resultSet.next()) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }

    }


}
